/**
 * Created by Administrator on 2017/2/11.
 */


import { Component,OnInit,Input,Output,EventEmitter} from '@angular/core';
import { Router, ActivatedRoute, Params } from '@angular/router';
import { PersonalService }     from './personal.serve'
import {FileUploader} from "ng2-file-upload";

import  'rxjs/add/operator/switchMap';

@Component({
    selector: 'my-personal',
    templateUrl: 'personal.html',
    providers:[ PersonalService ],
})
export class PersonalComponent implements OnInit {
  public uploader:FileUploader = new FileUploader({
        url: "http://api.pinwu.net/upload/uploadfile",
        method: "POST",
        itemAlias: "uploadedfile"
    });

  @Input() inputData;
  @Output() outputEvent:EventEmitter<string>=new EventEmitter();
  @Output() inputDataChange=new EventEmitter();
  constructor(
    private route: ActivatedRoute,
    private router: Router,
    private personalService:PersonalService,
  ) {}
  private inf='';
  private userid='';
  private errorMessage:string;
  private errMessage:string;
  private pers='';
  private collback='';
  private out='';
  private parentRouteId: number;
    selectedFileOnChanged(event:any) {
        console.log(event.target.value);
        this.uploadFile();
    }
    uploadFile() {
        // 上传
        this.uploader.queue[0].onSuccess = function (response, status, headers) {
            // 上传文件成功
            if (status == 200) {
                // 上传文件后获取服务器返回的数据
                let tempRes = JSON.parse(response);
            } else {
                // 上传文件后获取服务器返回的数据错误
                alert("");
            }
        };
        this.uploader.queue[0].upload(); // 开始上传
    }
  //获取用户名
  ngOnInit(){
    this.route.parent.params.subscribe(params=>{
      this.parentRouteId = +params["id"];
    });
    this.personalService.getname()
      .subscribe(
        res=>{this.inf=res['username'];
          this.userid=res['id'];
        }
      );
    // this.route.params
    //   .subscribe((params:Params)=>{this.inf= params['name'];
    //     this.userid=params['nameid'];
    // });
    //得到个人中心数据
    let useid = this.userid;
      this.personalService.personal(useid)
        .subscribe(
          res => {this.pers = res['data'];
          console.log(this.pers)
          },
          error =>this.errorMessage = <any>error);
  }
  //去文件夹的详情
  gocolltdeyail(id:string){
    this.router.navigate(['/linkCollect',id]);
  }
  //去制造厂姓名
  gofac(){
    let samename= this.inf;
    this.router.navigate(['/linkUpload',samename]);
  }
  //创建文件夹
  addcollt(colname:string,description:string){
    if (!colname || !description ) { return; }
    this.personalService.addcollect(colname,description)
      .subscribe(
        res => {this.collback = res;
          console.log(this.collback)
        },
        error =>  this.errMessage = <any>error);
  }
  //退出登录
  checkout(){
    // this.outputEvent.emit('sdasd'); //<---this will identify element index you are dealing with
    // this.inputDataChange.emit("Angular2");
    this.personalService.goout()
      .subscribe(
        res=>{this.out=res;
          localStorage.setItem('isOut','true');
          window['location'].href = 'http://localhost:4200/link-center';
        }
      );
     this.router.navigate(['/link-center'])
  }

  moreLink =false;
  more(){
    if (this.moreLink==true){
      this.moreLink=false
    }else {
      this.moreLink=true
    }
  }
}
